﻿using System.Security.Cryptography;
using System.Text;
using System;
using UnityEngine;
using System.IO;

namespace Client.Base
{
    public static class UniUtil
    {
        static private string key = "poker";
        static private byte[] AES_KEY;
        static private byte[] AES_IV;

        static UniUtil()
        {
            // 使用 SHA256 哈希算法生成 256 位密钥
            using (var sha256 = SHA256.Create())
            {
                AES_KEY = sha256.ComputeHash(Encoding.UTF8.GetBytes(key));
            }

            // 初始化向量（IV）
            AES_IV = new byte[16]; // AES 的块大小是 16 字节
            Buffer.BlockCopy(AES_KEY, 0, AES_IV, 0, AES_IV.Length); // 简单示例：用密钥的前 16 字节作为 IV
        }

        /// <summary>
        /// AES 加密字符串
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <returns></returns>
        static public string EncryptStringForAES(string source)
        {
            using (var aes = Aes.Create())
            {
                aes.Key = AES_KEY;
                aes.IV = AES_IV;
                var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

                using (var ms = new MemoryStream())
                {
                    using (var cryptoStream = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        using (var writer = new StreamWriter(cryptoStream))
                        {
                            writer.Write(source);
                        }
                        return Convert.ToBase64String(ms.ToArray());
                    }
                }
            }
        }

        /// <summary>
        /// AES 解密字符串
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <returns></returns>
        static public string DecryptyStringForAES(string source)
        {
            using (var aes = Aes.Create())
            {
                aes.Key = AES_KEY;
                aes.IV = AES_IV;
                var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
                using (var ms = new MemoryStream(Convert.FromBase64String(source)))
                {
                    using (var cryptoStream = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        using (var reader = new StreamReader(cryptoStream))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
        }

        /// <summary>
        /// AES 加密字节码
        /// </summary>
        /// <param name="source">源字节码</param>
        /// <returns></returns>
        static public byte[] EncryptBytesForAES(byte[] source)
        {
            using (var aes = Aes.Create())
            {
                aes.Key = AES_KEY;
                aes.IV = AES_IV;
                var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
                using (var ms = new MemoryStream())
                {
                    using (var cryptoStream = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(source, 0, source.Length);
                        cryptoStream.FlushFinalBlock(); 
                    }
                    return ms.ToArray(); 
                }
            }
        }

        /// <summary>
        /// AES 解密字节码
        /// </summary>
        /// <param name="source">源字节码</param>
        /// <returns></returns>
        static public byte[] DecryptyBytesForAES(byte[] source)
        {
            using (var aes = Aes.Create())
            {
                aes.Key = AES_KEY;
                aes.IV = AES_IV;
                var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                using (var ms = new MemoryStream(source))
                {
                    using (var cryptoStream = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        using (var msDecrypt = new MemoryStream())
                        {
                            cryptoStream.CopyTo(msDecrypt);
                            return msDecrypt.ToArray(); 
                        }
                    }
                }
            }
        }


    }

}